package com.by.shop.order.controller;

import com.alibaba.fastjson.JSON;
import com.by.shop.common.entity.Order;
import com.by.shop.common.entity.Product;
import com.by.shop.order.service.OrderService;
import com.by.shop.order.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("order")
public class OrderController {
    @Resource
    private OrderService orderService;
    // 在启动类中已经使用@Bean创建 RestTemplate对象，这里直接注入
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private ProductService productService;
    /*@GetMapping("prod/{pid}")
    public Order order(@PathVariable Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        // 使用restTemplate 查询信息
        Product product = restTemplate.getForObject("http://127.0.0.1:8081/product/findbyid/" + pid, Product.class);
        System.out.println("获得的商品信息为：" + JSON.toJSONString(product));
        // 如果商品有库存，下单
        if (product != null) {
            Order order = new Order();
            order.setUid(1);
            order.setUsername("测试用户");
            //BeanUtils.copyProperties(order, product);

            Order save = orderService.save(order);
            return save;
        }


        return null;
    }*/
    /*@GetMapping("prod/{pid}")
    public Order order(@PathVariable Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        ServiceInstance serviceInstance = discoveryClient.getInstances("service-product").get(0);
        String s = serviceInstance.getHost() + ":" + serviceInstance.getPort();
        Product product = restTemplate.getForObject("http://"+s+"/product/findbyid/" + pid, Product.class);
        System.out.println("获得的商品信息为：" + JSON.toJSONString(product));
        // 如果商品有库存，下单
        if (product != null) {
            Order order = new Order();
            order.setUid(1);
            order.setUsername("测试用户");
            //BeanUtils.copyProperties(order, product);

            Order save = orderService.save(order);
            return save;
        }


        return null;
    }*/

/*    @GetMapping("prod/{pid}")
    public Order order(@PathVariable Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        int i = new Random().nextInt(instances.size());
        ServiceInstance serviceInstance1 = instances.get(i);
        String s = serviceInstance1.getHost() + ":" + serviceInstance1.getPort();
        Product product = restTemplate.getForObject("http://" + s + "/product/findbyid/" + pid, Product.class);
        log.info(">>nacos获取的服务地址为"+s);
        System.out.println("获得的商品信息为：" + JSON.toJSONString(product));
        // 如果商品有库存，下单
        if (product != null) {
            Order order = new Order();
            order.setUid(1);
            order.setUsername("测试用户");
            //BeanUtils.copyProperties(order, product);

            Order save = orderService.save(order);
            return save;
        }


        return null;*/
        /*@GetMapping("prod/{pid}")
        public Order order (@PathVariable Integer pid){
            log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
            String s="service-product";
            *//*Product product = restTemplate.getForObject("http://" + s + "/product/findbyid/" + pid, Product.class);*//*
            ResponseEntity<Product> forEntity = restTemplate.getForEntity("http://" + s + "/product/findbyid/" + pid, Product.class);
            HttpStatus statusCode = forEntity.getStatusCode();
            int statusCodeValue = forEntity.getStatusCodeValue();
            Product body = forEntity.getBody();
            log.info("statusCode:"+statusCode);
            log.info("statusCodeValue:"+ statusCodeValue);
            log.info("body:"+body);
            System.out.println("获得的商品信息为：" + JSON.toJSONString(forEntity));
            // 如果商品有库存，下单
            if (forEntity != null) {
                Order order = new Order();
                order.setUid(1);
                order.setUsername("测试用户");
                //BeanUtils.copyProperties(order, product);

                Order save = orderService.save(order);
                return save;
            }


            return null;
        }*/
@GetMapping("prod/{pid}")
public Order order(@PathVariable Integer pid) {
    log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
    Product product = productService.findById(pid);
    log.info("获得的商品信息为：" + JSON.toJSONString(product));
    // 如果商品有库存，下单
    if (product != null) {
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        //BeanUtils.copyProperties(order, product);

        Order save = orderService.save(order);
        return save;
    }


    return null;
}
}
